home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / PACKING.ZIP / PACKING.TXT
Encoding:
Text File  |  1996-03-16  |  14.8 KB  |  318 lines

  1.  
  2.  
  3.             DATA COMPRESSION ALGORITHMS OF ARC, PKZIP, AND LHARC
  4.             ====================================================
  5.  
  6.  
  7.      Much of the typical  modem  user's  online  time  is  spent performing
  8.  uploads or  downloads of files from BBS's, Online Services like Compuserve
  9.  or GEnie, or Information  Networks like  Usenet or  Internet.   Given that
  10.  this  always  takes  up  a  lot  of time, and usually costs a considerable
  11.  amount of money, the need to  shorten the  time necessary  to perform file
  12.  transfers, and  other modem  applications has  always been prevalent.  One
  13.  innovation in this field has been  the development  of advanced Algorithms
  14.  for compacting,  or compressing  data so  it takes up much less space, and
  15.  packing multiple files into one Archive, or data  file, so  many files can
  16.  be sent at one time.
  17.  
  18.      The current  technology, an  offspring of data encryption methods used
  19.  in World War II, reduces the time it  takes to  transfer a  file through a
  20.  modem, by  reducing the  size of the data itself.  Given the proliferation
  21.  of many data compression methods (ARC, PKZIP, ZOO,  SIT, and  LHARC, for a
  22.  few  examples)  that  try  to  provide  the  most efficient method of data
  23.  compression, the topic has always been controversial in nature.
  24.  
  25.      Haruhiko Okumura provided  a  great  source  of  knowledge  about data
  26.  compression algorithms  by writing this essay, which describes some of the
  27.  effort involved in creating  a  data  compression  standard.    Except for
  28.  modifications in its formatting, or presentation, and various notes placed
  29.  in this text to provide more information on certain subjects,  the content
  30.  of Haruhiko Okumura's text is identical....
  31.  
  32.  
  33.  Introduction:  History of LHARC's Forefathers
  34.  ---------------------------------------------
  35.  
  36.      In the  spring of 1988, I wrote a very simple data compression program
  37.  named LZSS in C language, and uploaded it  to the  Science SIG  (forum) of
  38.  PC-VAN, Japan's biggest personal computer network.  That program was based
  39.  on Storer and Szymanski's slightly modified version of  one of  Lempel and
  40.  Ziv's algorithms.   Despite its simplicity, for most files its compression
  41.  outperformed the archivers then widely used.
  42.  
  43.      Kazuhiko Miki rewrote my LZSS in  Turbo Pascal  and assembly language,
  44.  and soon made it evolve into a complete archiver, which he named LARC. The
  45.  first versions of LZSS and LARC were rather  slow.   So I  rewrote my LZSS
  46.  using a binary tree, and so did Miki.  Although LARC's encoding was slower
  47.  than the fastest archiver available, its decoding was quite fast,  and its
  48.  algorithm was  so simple that even self-extracting files (compressed files
  49.  plus decoder) it created  were  usually  smaller  than non-self-extracting
  50.  files from other archivers. 
  51.  
  52.      Soon many  hobby programmers joined the archiver project at the forum.
  53.  Very many suggestions were made, and LARC was revised again and again.  By
  54.  the summer  of 1988,  LARC's speed  and compression  have improved so much
  55.  that LARC-compressed programs were beginning to be uploaded in many forums
  56.  of PC-VAN and other networks. 
  57.  
  58.      In that summer I wrote another program, LZARI, which combined the LZSS
  59.  algorithm with adaptive arithmetic  compression.   Although it  was slower
  60.  than LZSS,  its compression  performance was amazing.  Miki, the author of
  61.  LARC, uploaded LZARI to  NIFTY-Serve, another  big information  network in
  62.  Japan.    In  NIFTY-Serve,  Haruyasu  Yoshizaki  replaced LZARI's adaptive
  63.  arithmetic coding with a  version of  adaptive Huffman  coding to increase
  64.  speed.   Based on  this algorithm, which he called LZHUF, he developed yet
  65.  another archiver, LHarc. 
  66.  
  67.  
  68.  Data Compression Algorithms, Lempel-Ziv, and ARC.TTP
  69.  ----------------------------------------------------
  70.  
  71.        In what follows, I will review several of these algorithms and
  72.  supply simplified codes in C language.
  73.  
  74.  
  75.  1.  RLL Encoding
  76.  
  77.      Replacing several  (usually 8  or 4)  "space" characters  by one "tab"
  78.  character is a very primitive method for data compression.  Another simple
  79.  method is Run-Length coding  , which  encodes the  message "AAABBBBAACCCC"
  80.  into "3A4B2A4C", for example.
  81.  
  82.  
  83.  2. LZSS coding
  84.  
  85.      This scheme  is initiated  by Ziv and Lempel [1].  A slightly modified
  86.  version is described by Storer and Szymanski [2].  An implementation using
  87.  a binary  tree is  proposed by  Bell [3].   The algorithm is quite simple:
  88.  Keep a ring buffer,  which  initially  contains  "space"  characters only.
  89.  Read several  letters from the file to the buffer.  Then search the buffer
  90.  for the longest string that matches  the letters  just read,  and send its
  91.  length and position in the buffer. 
  92.  
  93.      If the  buffer size  is 4096  bytes, the position can be encoded in 12
  94.  bits.  If we represent the  match  length  in  four  bits,  the <position,
  95.  length> pair  is two bytes long.  If the longest match is no more than two
  96.  characters, then we send just one character without  encoding, and restart
  97.  the process with the next letter.  We must send one extra bit each time to
  98.  tell the decoder whether we are  sending a  <position, length>  pair or an
  99.  unencoded character. 
  100.  
  101.  
  102.  3. LZW coding
  103.  
  104.      This scheme  was devised  by Ziv and Lempel [4], and modified by Welch
  105.  [5].  The LZW coding has been adopted by most  of the  existing archivers,
  106.  such as  ARC and PKZIP.  The algorithm can be made relatively fast, and is
  107.  suitable for hardware implementation as well.  A  Pascal program  for this
  108.  algorithm is given in Storer's book [6]. 
  109.  
  110.  
  111.      The algorithm  can be  outlined as  follows: Prepare  a table that can
  112.  contain several  thousand items.   Initially  register in  its 0th through
  113.  255th positions  the usual  256 characters.  Read several letters from the
  114.  file to be encoded, and search the table for the  longest match.   Suppose
  115.  the longest  match is  given by  the string  "ABC".   Send the position of
  116.  "ABC" in the table.  Read the next character from the file.  If it is "D",
  117.  then register  a new  string "ABCD"  in the table, and restart the process
  118.  with the letter "D".  If the table becomes full,  discard the  oldest item
  119.  or, preferably, the least used. 
  120.  
  121.  
  122.  4. Huffman coding
  123.  
  124.      Classical  Huffman  coding  is  invented  by  Huffman  [7].   A fairly
  125.  readable account is given in  Sedgewick  [8].    Suppose  the  text  to be
  126.  encoded is  "ABABACA", with four A's, two B's, and a C.  We represent this
  127.  situation as follows:
  128.  
  129.                                  4    2    1
  130.                                  |    |    |
  131.                                  A    B    C
  132.  
  133.      Combine the least frequent two characters  into one,  resulting in the
  134.  new frequency 2 + 1 = 3:
  135.  
  136.                                  4      3
  137.                                  |     /  \
  138.                                  A    B    C
  139.  
  140.      Repeat the above step until the whole characters combine into a tree:
  141.  
  142.                                      7
  143.                                    /  \
  144.                                   /     3
  145.                                  /    /  \
  146.                                 A    B    C
  147.  
  148.      Start at  the top  ("root") of  this encoding  tree, and travel to the
  149.  character you want to encode.  If you go left, send a  "0"; otherwise send
  150.  a "1". Thus, "A" is encoded by "0", "B" by "10", "C" by "11".  Altogether,
  151.  "ABABACA" will be encoded  into ten  bits, "0100100110".   To  decode this
  152.  code,  the  decoder  must  know  the  encoding  tree,  which  must be sent
  153.  separately.
  154.  
  155.      A modification to this  classical Huffman  coding is  the adaptive, or
  156.  dynamic, Huffman  coding.   See, e.g.,  Gallager [9].  In this method, the
  157.  encoder and the decoder processes the first letter of the  text as  if the
  158.  frequency of  each character  in the  file were one, say.  After the first
  159.  letter has been processed,  both parties  increment the  frequency of that
  160.  character by  one.   For example,  if the  first letter  is 'C', then freq
  161.  ['C'] becomes two, whereas every other  frequencies are  still one.   Then
  162.  the both  parties modify  the encoding  tree accordingly.  Then the second
  163.  letter will be encoded and decoded, and so on.
  164.  
  165.  
  166.  5. Arithmetic coding
  167.  
  168.      The original concept of arithmetic coding is proposed by P. Elias.  An
  169.  implementation in C language is described by Witten and others [10]. 
  170.  
  171.      Although  the  Huffman  coding  is  optimal  if each character must be
  172.  encoded into a fixed (integer) number of bits,  arithmetic coding  wins if
  173.  no such restriction is made.
  174.  
  175.      As an  example we  shall encode  "AABA" using  arithmetic coding.  For
  176.  simplicity suppose we know beforehand that  the probabilities  for "A" and
  177.  B" to appear in the text are 3/4 and 1/4, respectively.
  178.  
  179.      Initially, consider an interval:
  180.  
  181.                              0 <= x < 1.
  182.  
  183.      Since the  first character  is "A" whose probability is 3/4, we shrink
  184.  the interval to the lower 3/4:
  185.  
  186.                              0 <= x < 3/4.
  187.  
  188.      The next character is "A" again, so we take the lower 3/4:
  189.  
  190.                              0 <= x < 9/16.
  191.  
  192.      Next comes "B" whose probability is 1/4, so we take the upper 1/4:
  193.  
  194.                              27/64 <= x < 9/16,
  195.  
  196.      Because "B" is the second element in our alphabet, {A, B}.  The last
  197.  character is "A" and the interval is
  198.  
  199.                              27/64 <= x < 135/256,
  200.  
  201.      which can be written in binary notation
  202.  
  203.                              0.011011 <= x < 0.10000111.
  204.  
  205.      Choose from this interval any number that can be represented in fewest
  206.  bits, say  0.1, and  send the  bits to  the right of "0."; in this case we
  207.  send only one bit, "1".  Thus we have encoded  four letters  into one bit!
  208.  With the  Huffman coding, four letters could not be encoded into less than
  209.  four bits. 
  210.  
  211.      To decode the code "1", we just reverse the process:  First, we supply
  212.  the "0."  to the  right of  the received  code "1",  resulting in "0.1" in
  213.  binary notation, or 1/2.  Since  this number  is in  the first  3/4 of the
  214.  initial interval  0 <= x < 1, the first character must be "A".  Shrink the
  215.  interval into the lower 3/4.  In this new interval, the number 1/2 lies in
  216.  the lower  3/4 part, so the second character is again "A", and so on.  The
  217.  number of letters in the original  file  must  be  sent  separately  (or a
  218.  special 'EOF' character must be appended at the end of the file).
  219.  
  220.      The  algorithm  described  above  requires  that  both  the sender and
  221.  receiver know  the  probability  distribution  for  the  characters.   The
  222.  adaptive  version  of  the  algorithm  removes  this  restriction by first
  223.  supposing uniform  or  any  agreed-upon  distribution  of  characters that
  224.  approximates  the  true  distribution,  and then updating the distribution
  225.  after each character is sent and received.
  226.  
  227.  
  228.  6. LZARI
  229.  
  230.      In each  step  the  LZSS  algorithm  sends  either  a  character  or a
  231.  <position, length>  pair.  Among these, perhaps character "e" appears more
  232.  frequently than "x", and a <position,  length> pair  of length  3 might be
  233.  commoner than one of length 18, say.  Thus, if we encode the more frequent
  234.  in fewer bits and the less frequent in more bits, the total  length of the
  235.  encoded text  will be diminished.  This consideration suggests that we use
  236.  Huffman or arithmetic coding, preferably of  an adaptive  kind, along with
  237.  LZSS.   This is  easier said  than done,  because there  are many possible
  238.  <position, length> combinations.   Adaptive compression  must keep running
  239.  statistics  of  frequency  distribution.    Too many items make statistics
  240.  unreliable.
  241.  
  242.  
  243.            LZARI, and the Creation of a Data Compression Program
  244.            -----------------------------------------------------
  245.  
  246.      What follows is not even an approximate solution to the  problem posed
  247.  above, but anyway this was what I did in the summer of 1988. 
  248.  
  249.      I extended  the character set from 256 to three-hundred or so in size,
  250.  and let characters 0 through 255  be the  usual 8-bit  characters, whereas
  251.  characters 253  + n represent that what follows is a position of string of
  252.  length n, where n = 3, 4 , ....  These extended set of  characters will be
  253.  encoded with adaptive arithmetic compression.
  254.  
  255.      I also  observed that  longest-match strings  tend to be the ones that
  256.  were read relatively recently.    Therefore,  recent  positions  should be
  257.  encoded into  fewer bits.   Since  4096 positions  are too  many to encode
  258.  adaptively,   I fixed  the probability  distribution of  the positions "by
  259.  hand".  The  distribution  function  given  in the accompanying LZARI.C is
  260.  rather tentative;  it  is  not  based  on  thorough  experimentation.   In
  261.  retrospect, I could encode adaptively the most significant 6 bits, say, or
  262.  perhaps  by  some  more  ingenious  method  adapt  the  parameters  of the
  263.  distribution function to the running statistics.
  264.  
  265.      At any  rate, the present version of LZARI treats the positions rather
  266.  separately, so that  the  overall  compression  is  by  no  means optimal.
  267.  Furthermore,  the  string  length  threshold above which strings are coded
  268.  into <position, length> pairs  is  fixed,  but  logically  its  value must
  269.  change according  to the  length of  the <position,  length> pair we would
  270.  get.
  271.  
  272.  
  273.  7. LZHUF
  274.  
  275.      LZHUF, the algorithm of Haruyasu Yoshizaki's  archiver LHarc, replaces
  276.  LZARI's adaptive  arithmetic coding  with adaptive Huffman.  LZHUF encodes
  277.  the most significant 6  bits of  the position  in its  4096-byte buffer by
  278.  table lookup.   More  recent, and hence more probable, positions are coded
  279.  in less bits.  On the other hand, the remaining 6 bits are sent verbatim.
  280.  
  281.      Because Huffman coding encodes each  letter  into  a  fixed  number of
  282.  bits,  table  lookup  can  be  easily  implemented.   Though theoretically
  283.  Huffman cannot  exceed  arithmetic  compression,  the  difference  is very
  284.  slight, and LZHUF is fairly fast. 
  285.  
  286.  
  287.  References:
  288.  -----------
  289.  
  290.    [1] J. Ziv and A. Lempel, IEEE Trans. IT-23, 337-343 (1977).
  291.  
  292.    [2] J. A. Storer and T. G. Szymanski, J. ACM, 29, 928-951
  293.        (1982).
  294.  
  295.    [3] T. C. Bell, IEEE Trans. COM-34, 1176-1182 (1986).
  296.  
  297.    [4] J. Ziv and A. Lempel, IEEE Trans. IT-24, 530-536 (1978).
  298.  
  299.    [5] T. A. Welch, Computer, 17, No.6, 8-19 (1984).
  300.  
  301.    [6] J. A. Storer, Data Compression: Methods and Theory
  302.        (Computer Science Press, 1988).
  303.  
  304.    [7] D. A. Huffman, Proc IRE 40, 1098-1101 (1952).
  305.  
  306.    [8] R. Sedgewick, Algorithms, 2nd ed. (Addison-Wesley, 1988).
  307.  
  308.    [9] R. G. Gallager, IEEE Trans. IT-24, 668-674 (1978).
  309.  
  310.   [10] I. E. Witten, R. M. Neal, and J. G. Cleary, Commun. ACM
  311.        30, 520-540 (1987).
  312.          ________________________________________________________
  313.  
  314.                   Utdrag ur ST Report av den 15 mars 1991
  315.  
  316.                      29 mars 1991 - Ghlenn Willard 6929
  317.  
  318.